﻿using System;
using System.Collections;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Rectangle = iTextSharp.text.Rectangle;

namespace Test1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: данная строка кода позволяет загрузить данные в таблицу "diplomkaDataSet.country". При необходимости она может быть перемещена или удалена.
            this.countryTableAdapter.Fill(this.diplomkaDataSet.country);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //for (int i = 0; i < dataGridView1.RowCount; i++)
            //{
            //    if (Convert.ToString(dataGridView1[2, i].Value) == "")
            //    {
            //        MessageBox.Show("False");
            //    }
            //    else
            //    {
            //        MessageBox.Show(Convert.ToString(dataGridView1[2, i].Value));
            //    }
            //}
            
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                if (Convert.ToString(dataGridView1[2, i].Value) == "" || Convert.ToBoolean(dataGridView1[2, i].Value) == false)
                {
                    //MessageBox.Show(Convert.ToString(i));
                    dataGridView1.Rows.RemoveAt(i);
                }
            }
            
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (Convert.ToString(dataGridView1[2, e.RowIndex].Value) == "" || Convert.ToBoolean(dataGridView1[2, e.RowIndex].Value)==false)
            {
                dataGridView1[2, e.RowIndex].Value = true;
            }
            else
            {
                dataGridView1[2, e.RowIndex].Value = false;
            }
            MessageBox.Show(Convert.ToString(dataGridView1[2, e.RowIndex].Value));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Convert.ToString(dataGridView1.RowCount));
            FileStream fs = new FileStream(Application.StartupPath + "\\1.pdf", FileMode.Create, FileAccess.ReadWrite, FileShare.None);
            var a = new Rectangle(842f, 595f);
            var doc = new Document(a);
            //doc.SetPageSize(PageSize.A4_LANDSCAPE);
            PdfWriter.GetInstance(doc, fs);
            PdfPTable table = new PdfPTable(2);
            //actual width of table in points
            table.TotalWidth = 216f;
            //fix the absolute width of the table
            table.LockedWidth = true;
            //relative col widths in proportions - 1/3 and 2/3
            float[] widths = new float[] { 1f, 3f };
            table.SetWidths(widths);
            table.HorizontalAlignment = 0;
            //leave a gap before and after the table
            table.SpacingBefore = 70f;
            table.SpacingAfter = 30f;
            PdfPCell cell = new PdfPCell(new Phrase("Bolum\n"));

            cell.Colspan = 5;
            cell.Border = 0;
            cell.HorizontalAlignment = 0;
            table.AddCell(cell);
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                table.AddCell(Convert.ToString(dataGridView1[0, i].Value));
                table.AddCell(Convert.ToString(dataGridView1[1, i].Value));
            }
            doc.Open();
            doc.Add(new PdfDiv());
            doc.Add(table);
            doc.Close();
        }
    }
}
